﻿namespace WXWorkSvc.Common.TagHelpers;
/// <summary>
/// 一般用在Index List 的前面，按照字典的key排序
/// </summary>

[HtmlTargetElement("wx-select-link", TagStructure = TagStructure.WithoutEndTag)]
public class WxSelectLinkTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    private readonly IHtmlGenerator _HtmlGenerator;

    public WxSelectLinkTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression?> SelectFors { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-select-")]
    public Dictionary<string, IEnumerable<SelectListItem>> SelectLists { get; set; } = new();

    [HtmlAttributeName(DictionaryAttributePrefix = "asp-link-text-")]
    public Dictionary<string, string> PageLinkTexts { get; set; } = new();
    /// <summary>
    /// 若为空，则onclick(asp-route-values..)
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-page-")]
    public Dictionary<string, string> PageNames { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-fun-")]
    public Dictionary<string, string> FunctionNames { get; set; } = new();
    /// <summary>
    /// 常量参数
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-const-")]
    public Dictionary<string, object> ConstValues { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-route-")]
    public Dictionary<string, ModelExpression> RouteValues { get; set; } = new();
    //[HtmlAttributeName(DictionaryAttributePrefix = "asp-link-type-")]
    //public Dictionary<string, EButtonType> LinkTypes { get; set; } = new();
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", "weui-cells");

        Dictionary<string, TagBuilder> tags = new();
        foreach (var kv in SelectFors)
        {
            if (SelectLists.Remove(kv.Key, out var selectList))
            {
                var selectTagBuilder = new TagBuilder("div");
                selectTagBuilder.InnerHtml.SetHtmlContent(string.Format(TagHelperExtend.WeuiCellSelectFormat,
                    kv.Value!.GetDisplayName(),
                    _HtmlGenerator.GenerateSelect(ViewContext, kv.Value!.ModelExplorer, null, kv.Value.Name, selectList, false, new { @class = "weui-select" }).GetHtmlString()
                    ));
                tags.Add(kv.Key, selectTagBuilder);
            }
        }

        foreach (var kv in PageLinkTexts)
        {
            var routeValues = RouteValues.GetRouteValues(ConstValues, kv.Key);
            if (PageNames.Remove(kv.Key, out var pageName))
            {
                var linkTagBuilder = _HtmlGenerator.GeneratePageLink(ViewContext, kv.Value, pageName, null, null, null, null, routeValues, new { @class = TagHelperExtend.WeuiCellLinkClass });
                linkTagBuilder.InnerHtml.SetHtmlContent(string.Format(TagHelperExtend.WeuiCellLinkText, kv.Value));
                tags.Add(kv.Key, linkTagBuilder);

            }
            else if (FunctionNames.Remove(kv.Key, out var functionName))
            {

                var linkTagBuilder = new TagBuilder("a");
                linkTagBuilder.AddCssClass(TagHelperExtend.WeuiCellLinkClass);
                linkTagBuilder.Attributes.Add("href", "javascript:;");
                linkTagBuilder.Attributes.Add("onclick", routeValues.ToFunction(functionName));
                linkTagBuilder.InnerHtml.SetHtmlContent(string.Format(TagHelperExtend.WeuiCellLinkText, kv.Value));
                tags.Add(kv.Key, linkTagBuilder);
            }
        }

        foreach (var kv in tags.OrderBy(ii => ii.Key))
            output.Content.AppendHtml(kv.Value);
    }
}
